package com.gitee.wsl

import com.gitee.wsl.statemachine.stateMachine
import kotlinx.coroutines.GlobalScope
import kotlin.test.Test


class StateMachineTest {

    @Test
    fun testStateMachine(){
        defaultStateMachine.action(DefaultAction.ChangeAction)
    }

}

/**
 * 示便 应用
 */
class DefaultState {
    companion object{
        val Base=DefaultState()
        val False=DefaultState()
    }
}

class DefaultAction {
    companion object{
        val ChangeAction=DefaultAction()
        val EndAction=DefaultAction()
    }
}

val defaultStateMachine= stateMachine<DefaultAction,DefaultState>(scope = GlobalScope,DefaultState.Base){
    /*this.reducer={action, state ->
        println("on action  :$action, state:${state}")
        when(action to state){
            DefaultAction.ChangeAction to DefaultState.Base->{
                println("on action change :$action, state:${state}")
                this.transitionTo(DefaultState.False)
            }
            DefaultAction.EndAction to DefaultState.Base->{
                println("on action end :$action, state:${state}")
            }
        }
    }*/
    this.onStateChanged={state ->
        println("Currnet state:${state}")
    }

   state( DefaultState.Base){
        action(DefaultAction.ChangeAction,DefaultState.False){
            println("on action  :${DefaultAction.ChangeAction}, state:${DefaultState.Base} to ${DefaultState.False}")
        }

        trigger {
           tryAction { beforeState, toState ->
               println("try action state from:${beforeState} to $toState")
               true
           }
            entry { beforeState, toState ->
                println("entry state ${DefaultState.Base}")
            }
            leave {beforeState, toState ->
            println("leave state ${DefaultState.Base}")
            }
        }
    }

    state(DefaultState.False){
        trigger {
            entry {beforeState, toState ->
              println("entry state ${DefaultState.False}")
            }
            leave {beforeState, toState ->
              println("leave state ${DefaultState.False}")
            }
        }
    }
}